package cn.devzyh.controller;

import cn.devzyh.domain.Resource;
import cn.devzyh.domain.ResourceVo;
import cn.devzyh.domain.ResultVo;
import cn.devzyh.service.ResourceService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/resource")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    // 资源信息分页&条件查询
    @PostMapping("/findAllResource")
    public ResultVo findAllResource(@RequestBody ResourceVo resourceVo) {

        PageInfo<Resource> pageInfo = resourceService.findByConditionAndPage(resourceVo);

        return new ResultVo(true, 200, "响应成功", pageInfo);
    }

    // 添加&更新资源信息
    @PostMapping("/saveOrUpdateResource")
    public ResultVo saveOrUpdateResource(@RequestBody Resource resource) {

        ResultVo res = new ResultVo(true, 200, "", null);

        if (null == resource.getId()) {

            resourceService.saveResource(resource);
            res.setMessage("新增资源信息成功");
        } else {

            resourceService.updateResource(resource);
            res.setMessage("修改资源信息成功");
        }

        return res;
    }

    // 删除资源信息
    @GetMapping("/deleteResource")
    public ResultVo deleteResource(@RequestParam(value = "id", required = true) Integer id) {

        resourceService.deleteResource(id);

        return new ResultVo(true, 200, "删除资源信息成功", null);
    }
}
